package view.plotter

import groovy.swing.SwingBuilder
import groovy.transform.CompileStatic

import java.awt.BorderLayout
import java.awt.Color

import javax.swing.JFrame

import org.jfree.chart.ChartFactory
import org.jfree.chart.ChartPanel
import org.jfree.chart.JFreeChart
import org.jfree.chart.axis.NumberAxis
import org.jfree.chart.plot.PlotOrientation
import org.jfree.chart.plot.XYPlot
import org.jfree.chart.title.LegendTitle
import org.jfree.data.xy.XYSeries
import org.jfree.data.xy.XYSeriesCollection
import org.jfree.ui.RectangleEdge
import org.jfree.ui.RefineryUtilities

//@CompileStatic
class Plotter {

	XYSeriesCollection dataset = new XYSeriesCollection()

	def datasetList = []
	
	JFreeChart chart
	

	def pens = [:]

	def ticks = 0

	def title
	
	def setTitle(String t){
		title = t
		chart.setTitle(t)
	}

	def getTitle(){
		return title
	}


	def Plotter(String title){
		
		chart = createChart();
		
		setTitle(title)
		//chart.setTitle(title)

	}

	def show(){
		def swing = new SwingBuilder();
		def frame = swing.frame(title: "绘图面板" ,
		//size:[400, 600],
		//location:[x, y],
		//windowClosed:windowClosedHandler,
		defaultCloseOperation:javax.swing.WindowConstants.DISPOSE_ON_CLOSE){
			panel(layout: new BorderLayout()){
				//dataset = createDataset();

				ChartPanel chartPanel = new ChartPanel(chart);
				chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
				widget(chartPanel);
			}
		}

		frame.pack()
		RefineryUtilities.centerFrameOnScreen(frame);
		frame.setVisible(true)
	}



	def setPenColor(String name,Color c){
		if(!penExist(name)){throw new IllegalArgumentException("画笔${name}不存在！")}
		def i = pens.findIndexOf{it.key == name}
		chart.getPlot().getRenderer().setSeriesPaint(i, c);
	}

	def plot(name,value){
		
		if(!penExist(name)){
			//println "${title}中画笔有${pens}！"
			throw new IllegalArgumentException("${title}中画笔${name}不存在！")
			}
		XYSeries s = pens[name]
		s.add(ticks,value)
		ticks++
	}

	private JFreeChart createChart() {
		// create the chart...
		
		JFreeChart chart = ChartFactory.createXYLineChart("title", // chart
				// title
				"X", // x axis label
				"Y", // y axis label
				dataset, // data
				PlotOrientation.VERTICAL, true, // include legend
				true, // tooltips
				false // urls
				);
		
		chart.setBackgroundPaint(Color.lightGray);
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.white);
		//plot.setAxisOffset(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
		plot.setDomainGridlinePaint(Color.gray);
		plot.setRangeGridlinePaint(Color.gray);
		//XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
		//		renderer.setShapesVisible(true);
		//		renderer.setShapesFilled(true);
		// change the auto tick unit selection to integer units only...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		//rangeAxis.setTickLabelsVisible(false)
		rangeAxis.setLabel("")
		//NumberAxis rangeAxis = (NumberAxis) plot.get
		NumberAxis yAxis = (NumberAxis)plot.getDomainAxis()
		yAxis.setLabel("")
		//rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		// OPTIONAL CUSTOMISATION COMPLETED.
		LegendTitle legend = chart.getLegend();
		legend.setPosition(RectangleEdge.RIGHT);
		return chart;
	}

	def createSubPlot(){
		XYPlot plot = new XYPlot()
		plot.setBackgroundPaint(Color.white);
		//plot.setAxisOffset(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
		plot.setDomainGridlinePaint(Color.gray);
		plot.setRangeGridlinePaint(Color.gray);
		//XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
		//		renderer.setShapesVisible(true);
		//		renderer.setShapesFilled(true);
		// change the auto tick unit selection to integer units only...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		//rangeAxis.setTickLabelsVisible(false)
		rangeAxis.setLabel("")
		//NumberAxis rangeAxis = (NumberAxis) plot.get
		NumberAxis yAxis = (NumberAxis)plot.getDomainAxis()
		yAxis.setLabel("")
		//rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		// OPTIONAL CUSTOMISATION COMPLETED.
		LegendTitle legend = chart.getLegend();
		legend.setPosition(RectangleEdge.RIGHT);
		return plot
	}
	
	def addPen(String name,int plot = 0){
		if(penExist(name)){
			println "画笔已存在"
			return}

		XYSeries s = new XYSeries(name);
		pens[name] = s
		dataset.addSeries(s)
//		println "添加画笔${name}"
//		println pens

	}
	
	def plotDemo(){

		addPen("sin(x)");
		addPen("cos(x)");
		addPen("zero");
		setPenColor("zero",Color.black);

		for(int i=-100;i<100;i++){
			float x = (float)i / 10 ;

			plot("sin(x)",Math.sin(x));
			plot("cos(x)",Math.cos(x));
			plot("zero",0);
		}
	}

	def clear(){
		//dataset = new XYSeriesCollection();
		dataset.removeAllSeries()
		pens.each { name ,XYSeries s ->
			s.clear()
			ticks = 0

		}
		
		pens = [:]
	}
	
	def reset(){
		clear()
		//chart.plot = new 
	}


	def penExist(String name){
		if(pens.keySet()!= null){

			return pens.keySet().contains(name)
		}
		return false
	}

}

class Pen{
}